
// mfcPuppyDlg.cpp : implementation file
//

#include "stdafx.h"
#include "mfcPuppy.h"
#include "mfcPuppyDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
public:
//	afx_msg void OnSize(UINT nType, int cx, int cy);
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
//	ON_WM_SIZE()
END_MESSAGE_MAP()


// CmfcPuppyDlg dialog




CmfcPuppyDlg::CmfcPuppyDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CmfcPuppyDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

	m_Mass = 5.0;
	m_Zeta = 1.0;

	m_k11 = 50000.0;	m_k12 = 0.0;		m_k13 = 0.0;
	m_k21 = 0.0;		m_k22 = 50000.0;	m_k23 = 0.0;
	m_k31 = 0.0;		m_k32 = 0.0;		m_k33 = 50000.0;

	m_b11 = 1000.0;		m_b12 = 0.0;		m_b13 = 0.0;
	m_b21 = 0.0;		m_b22 = 1000.0;		m_b23 = 0.0;
	m_b31 = 0.0;		m_b32 = 0.0;		m_b33 = 1000.0;

	m_tarFx = 0.0;		m_tarPx = 0.0;
	m_tarFy = 0.0;		m_tarPy = 0.0;
	m_tarFz = 0.0;		m_tarPz = 0.0;
	m_tarRoll = 0.0;	m_tarPitch = 0.0;	m_tarYaw = 0.0;
}

void CmfcPuppyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_lstHMName, m_lstHMName);
	DDX_Control(pDX, IDC_lstSyncRate, m_lstSyncRate);
	DDX_Control(pDX, IDC_lstSampleRate, m_lstSampleRae);

	DDX_Text(pDX, IDC_edtMass, m_Mass);		DDV_MinMaxDouble(pDX, m_Mass, 0.5, 20.0);
	DDX_Text(pDX, IDC_edtZeta, m_Zeta); 	DDV_MinMaxDouble(pDX, m_Zeta, -1.0, 1.0);

	DDX_Text(pDX, IDC_k11, m_k11);			DDV_MinMaxDouble(pDX, m_k11, -5000, 50000);
	DDX_Text(pDX, IDC_k12, m_k12);			DDV_MinMaxDouble(pDX, m_k12, -5000, 50000);
	DDX_Text(pDX, IDC_k13, m_k13);			DDV_MinMaxDouble(pDX, m_k13, -5000, 50000);
	DDX_Text(pDX, IDC_k21, m_k21);			DDV_MinMaxDouble(pDX, m_k21, -5000, 50000);
	DDX_Text(pDX, IDC_k22, m_k22);			DDV_MinMaxDouble(pDX, m_k22, -5000, 50000);
	DDX_Text(pDX, IDC_k23, m_k23);			DDV_MinMaxDouble(pDX, m_k23, -5000, 50000);
	DDX_Text(pDX, IDC_k31, m_k31);			DDV_MinMaxDouble(pDX, m_k31, -5000, 50000);
	DDX_Text(pDX, IDC_k32, m_k32);			DDV_MinMaxDouble(pDX, m_k32, -5000, 50000);
	DDX_Text(pDX, IDC_k33, m_k33);			DDV_MinMaxDouble(pDX, m_k33, -5000, 50000);

	DDX_Text(pDX, IDC_b11, m_b11);			DDV_MinMaxDouble(pDX, m_b11, -5000, 50000);
	DDX_Text(pDX, IDC_b12, m_b12);			DDV_MinMaxDouble(pDX, m_b12, -5000, 50000);
	DDX_Text(pDX, IDC_b13, m_b13);			DDV_MinMaxDouble(pDX, m_b13, -5000, 50000);
	DDX_Text(pDX, IDC_b21, m_b21);			DDV_MinMaxDouble(pDX, m_b21, -5000, 50000);
	DDX_Text(pDX, IDC_b22, m_b22);			DDV_MinMaxDouble(pDX, m_b22, -5000, 50000);
	DDX_Text(pDX, IDC_b23, m_b23);			DDV_MinMaxDouble(pDX, m_b23, -5000, 50000);
	DDX_Text(pDX, IDC_b31, m_b31);			DDV_MinMaxDouble(pDX, m_b31, -5000, 50000);
	DDX_Text(pDX, IDC_b32, m_b32);			DDV_MinMaxDouble(pDX, m_b32, -5000, 50000);
	DDX_Text(pDX, IDC_b33, m_b33);			DDV_MinMaxDouble(pDX, m_b33, -5000, 50000);

	DDX_Text(pDX, IDC_tarFx, m_tarFx);		DDV_MinMaxDouble(pDX, m_tarFx, -50, 50);
	DDX_Text(pDX, IDC_tarFy, m_tarFy);		DDV_MinMaxDouble(pDX, m_tarFy, -50, 50);
	DDX_Text(pDX, IDC_tarFz, m_tarFz);		DDV_MinMaxDouble(pDX, m_tarFz, -50, 50);
	
	DDX_Text(pDX, IDC_tarPx, m_tarPx);		DDV_MinMaxDouble(pDX, m_tarPx, -0.5, 0.5);
	DDX_Text(pDX, IDC_tarPy, m_tarPy);		DDV_MinMaxDouble(pDX, m_tarPy, -0.5, 0.5);
	DDX_Text(pDX, IDC_tarPz, m_tarPz);		DDV_MinMaxDouble(pDX, m_tarPz, -0.5, 0.5);

	DDX_Text(pDX, IDC_tarRoll, m_tarRoll);		DDV_MinMaxDouble(pDX, m_tarRoll, -90.0, 90.0);
	DDX_Text(pDX, IDC_tarPitch, m_tarPitch);	DDV_MinMaxDouble(pDX, m_tarPitch, -90.0, 90.0);
	DDX_Text(pDX, IDC_tarYaw, m_tarYaw);		DDV_MinMaxDouble(pDX, m_tarYaw, -90.0, 90.0);
}

BEGIN_MESSAGE_MAP(CmfcPuppyDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_btnConnect, &CmfcPuppyDlg::OnBnClickedbtnconnect)
END_MESSAGE_MAP()


// CmfcPuppyDlg message handlers

BOOL CmfcPuppyDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	//SetIcon(m_hIcon, FALSE);		// Set small icon

	/// <summary>
	///	Initialize the list boxes
	/// </summary>

	// hm list box
	m_lstHMName.AddString(L"vmd");
	m_lstHMName.AddString(L"emu");
	m_lstHMName.SetCurSel(0);
	
	// sync rate box
	m_lstSyncRate.AddString(L"125");
	m_lstSyncRate.AddString(L"250");
	m_lstSyncRate.AddString(L"500");
	m_lstSyncRate.AddString(L"1250");
	m_lstSyncRate.SetCurSel(3);

	// sample rate box
	m_lstSampleRae.AddString(L"250");
	m_lstSampleRae.AddString(L"500");
	m_lstSampleRae.AddString(L"1250");
	m_lstSampleRae.AddString(L"2500");
	m_lstSampleRae.SetCurSel(3);

	CButton* pButton = (CButton*)GetDlgItem(IDC_radRatio);
	pButton->SetCheck(true);


	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CmfcPuppyDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CmfcPuppyDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CmfcPuppyDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

void CmfcPuppyDlg::OnBnClickedbtnconnect()
{
	/// <summary>
	/// Connect to the HapticMaster in a separate thread
	/// Start the OpenGL display thread
	/// </summary>

	m_displayDialog = new CDisplayDialog;
	if (m_displayDialog != NULL) {
		if ( m_displayDialog->Create(IDD_DISPLAYDIALOG, this) )
			m_displayDialog->ShowWindow(SW_SHOW);
		else
			AfxMessageBox(_T("Error creating displayDialog"));
	}
	else
		AfxMessageBox(_T("Error creating displayDialog"));
	
}

